'use strict';

const Controller = require('egg').Controller;

class ScoreController extends Controller {
    //添加
    async create() {
        let {ctx,service} = this;
        let {theory,skill,num,username } = ctx.request.body;

        //1.容错
        if((theory >=0 && theory <= 100) && (skill >=0 && skill <= 100) && num && username){
            try{
                await service.score.create(theory,skill,num,username);
                ctx.body = {
                    code:1,
                    msg:'添加成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //修改
    async update(){
        let {ctx,service} = this;
        let {theory,skill} = ctx.request.body;

        let {id} = ctx.params;

        //1.容错
        if((theory >=0 && theory <= 100) && (skill >=0 && skill <= 100) && id){
            try{
                await service.score.update(theory,skill,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //删除
    async destroy(){
        let {ctx,service} = this;
        let {id} = ctx.params;
        if(id){
            try{
                await service.score.destroy(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //查询 今天成绩   pagenum=1   limit=2
    async index(){
        let{ctx,service} = this;
        // let {pagenum=1,limit=2} = ctx.query; 
        //查询今天的成绩列表
        let list = await service.score.index();
        ctx.body = {
            code:1,
            data:list
        }
    }
    //搜索
    async search(){
        let {ctx,service} = this;
        let {key} = ctx.query;
        if(key){
            let searchList = await service.score.search(key);
            ctx.body = {
                code:1,
                data:searchList
            }
        }else{
            ctx.body = {
                code:2,
                msg:"参数有误"
            }
        }
    }
    //今天  没有录入成绩  的人员列表   设计到两表联查  
    async notScore(){
        let {ctx,service} = this;
        let notScoreList = await service.score.notScore();
        ctx.body = {
            code:1,
            data:notScoreList
        }
    }

    // 获取理论和技能在各个区间的人数
    async bar(){
        let {ctx,service} = this;
        //['0-70','71-80','81-90','91-100'];

        let arr = ['0-70','71-80','81-90','91-100'];

        //理论人数数组  技能人数的数组
        let theory = [],skill=[];

        for(let i = 0;i<arr.length;i++){
            let scoreArr = arr[i].split('-');
            let min = scoreArr[0];
            let max = scoreArr[1];
            let tCount = await service.score.bar(min,max,'theory');
            let SCount = await service.score.bar(min,max,'skill');
            theory.push(tCount[0]['count(*)']);
            skill.push(SCount[0]['count(*)']);
        }

        ctx.body = {
            code:1,
            data:{
                theory,
                skill
            }
        }

    }

    // 获取这个月折线图数据
    async line(){
        let {ctx,service} = this;
        let {num} = ctx.query;
        let list = await service.score.line(num);
        let theoryArr = [],skillArr=[],timeArr=[];
        list.sort((a,b) => {
            return a.id -b.id
        })
        list.forEach(item => {
            theoryArr.push(item.theory);
            skillArr.push(item.skill);
            timeArr.push(item.score_time);
        })
        ctx.body = {
            code:1,
            data:{
                theoryArr,
                skillArr,
                timeArr
            }
        }
    }
}

module.exports = ScoreController;
